#include "RingQueue.hpp"
#include "Task.hpp"
#include <ctime>
#include <unistd.h>
#include <cstring>
const char *ops = "+-*/%";

void *ProductorR(void *args){
    RingQueue<Task> *rq = static_cast<RingQueue<Task> *> (args);
    while(true){
        int x = rand() % 100;
        int y = rand() % 100;
        char op = ops[(x + y) % strlen(ops)];
        Task task(x, y, op);
        rq->push(task);
        cout << "productor done: " << task.formatArg() << endl;
    }
}

void *ConsumerR(void *args){
    RingQueue<Task> *rq = static_cast<RingQueue<Task> *> (args);
    while(true){
        Task task;
        rq->pop(&task);
        task();
        cout << "consumer done: " << task.formatRes() << endl;
    }
}

int main(){
    srand((uint64_t)time(nullptr) ^ getpid());
    pthread_t productor, consumer;
    RingQueue<Task> *rq = new RingQueue<Task>;
    pthread_create(&productor, nullptr, ProductorR, rq);
    pthread_create(&consumer, nullptr, ConsumerR, rq);

    pthread_join(productor, nullptr);
    pthread_join(consumer, nullptr);

    delete rq;
    return 0;
}